package com.app.view.component.room {
	import com.peachcastle.core.Display;
	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.events.Event;
	
	import com.app.view.component.RoomMediator;
	import org.component.Slider;
	
	import com.peachcastle.tools.Utils;
	
	import com.greensock.TweenLite;
	
	public class Chat extends Display 
	{
		private var _chatSendBtn:SimpleButton = null;
 	
		private var _roomMediator:RoomMediator = null;
		private var _inputText:TextField = null;    // 内容输入框
		private var _slider:Slider = null;          // 滚动条
		private var _inputChat:MovieClip = null;
		private var _showChat:MovieClip = null;
		private var _showChatzz:MovieClip = null;
		private var _showChatbg:MovieClip = null;
		
		public function Chat(ui:MovieClip, roomMediator:RoomMediator)
		{
			_roomMediator = roomMediator;
			
			super(ui);
		}
		
		override protected function initialization():void
		{
			super.initialization();
			
			_inputChat	= assest["inputChat"];
			_showChat = assest["showChat"];
			_chatSendBtn = _inputChat["btn_chat_send"];
			_inputText = _showChat["inputText"];			
			_slider = new Slider(_showChat["sliderView"]);
			_showChatzz = assest["showChatzz"];
			_showChatbg = assest["showChatbg"];
			
			_inputText.maxChars = 200;
			_showChatzz.height = 22;
			_showChatzz.y = 70;
			_showChatbg.y = 70;
		}
		
		override protected function configEventListeners():void
		{
			_chatSendBtn.addEventListener(MouseEvent.MOUSE_DOWN, mouseChat);
			
			_inputText.addEventListener(Event.CHANGE, onInputChange); 
			_slider.addEventListener(Event.CHANGE, onChange);
			//增加隐藏和显示
			
			assest.addEventListener(MouseEvent.MOUSE_OVER, showChat);
			assest.addEventListener(MouseEvent.MOUSE_OUT, hideChat);
			assest.addEventListener(KeyboardEvent.KEY_DOWN, keyChat);
		}
		
		private function keyChat(event:KeyboardEvent):void
		{
			if (event.charCode == 13 || event.keyCode == 13)
			{
				sendText();
			}
		}
		private function mouseChat(e:MouseEvent):void
		{
			sendText();
		}
		private function sendText():void
		{
			_inputChat["txt_chatTxt"].text=_inputChat["txt_chatTxt"].text.replace(/([ ]{0,})/g,"");
			if (_inputChat["txt_chatTxt"].text != "")
			{
				_roomMediator.sendNotification(RoomMediator.CHAT_REQUEST, [_inputChat["txt_chatTxt"].text]);
				_inputChat["txt_chatTxt"].text = "";
			}
		}
		
		public function writeText(str:String):void
		{
			_showChat["inputText"].text = _showChat["inputText"].text + "\n" +str;
			if(_showChat["inputText"].numLines>5){
				var m:int=_showChat["inputText"].getLineOffset(1);
				_showChat["inputText"].replaceText(0,m,"");
			}
		}
		
		private function onInputChange(e:Event):void 
		{
			_inputText.scrollV = _inputText.maxScrollV;
			_slider.update(5, _inputText.numLines);
			_slider.value = 100;
		}
		
		/**
		 * 滑动条改变事件处理函数
		 * @param	e
		 */
		private function onChange(e:Event):void 
		{
			var value:Number = _slider.value;
			_inputText.scrollV = Math.round(value / 100 * _inputText.maxScrollV);
		}
		//聊天框升起
		private function showChat(e:MouseEvent):void
		{
			TweenLite.to(_showChatzz, 1, { height:92,y:0 } );
			TweenLite.to(_showChatbg, 1, { y:0 } );
		}
		//聊天框隐藏
		private function hideChat(e:MouseEvent):void
		{
			TweenLite.to(_showChatzz, 1, { height:22,y:70} );
			TweenLite.to(_showChatbg, 1, { y:70 } );
		}
	}
}